package com.caigou.controller;

import com.caigou.annotation.OperLog;
import com.caigou.bean.cms.entity.Picture;
import com.caigou.bean.cms.entity.PictureFolder;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.exam.entity.Users;
import com.caigou.constant.NewsConstant;
import com.caigou.enums.ResponseEnum;
import com.caigou.security.utils.CurrentUser;
import com.caigou.service.PictureService;
import com.caigou.util.Result;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/picture/")
public class PictureController {

    @Resource
    private PictureService pictureService;


    @RequestMapping("queryPicture")
    @PreAuthorize("hasAnyAuthority('tupian_quankuchaxun')")
    public Result<PageInfo<Picture>> queryPicture(@RequestBody PictureQueryParam pictureQueryParam, @CurrentUser Users user){
        PageInfo<Picture> pageInfoPicture = pictureService.queryPictureIncludingSubfolders(pictureQueryParam, user);
        //PageInfo<Picture> pageInfoPicture = pictureService.queryPictureIncludingSubfolders(folderId, keyword, pageNum, pageSize);
        return Result.success(pageInfoPicture);
    }


    /**
     * 获取图片库根目录
     * @return
     */
    @RequestMapping("getRootPictureFolder")
    @PreAuthorize("hasAnyAuthority('tupian_quankuchaxun')")
    public Result getRootPictureFolder(@CurrentUser Users user){
        PictureFolder rootPictureFolder = pictureService.getRootPictureFolder();
        return Result.success(rootPictureFolder);
    }

    /**
     * 获取某一图片库目录下的图片目录列表
     * @param pictureFolderParam
     * @return
     */
    @RequestMapping("listPictureFolderBySuperId")
    @PreAuthorize("hasAnyAuthority('tupian_quankuchaxun')")
    public Result listPictureFolderBySuperId(@RequestBody PictureFolderParam pictureFolderParam, @CurrentUser Users user){
        List<PictureFolder> pictureFolderList = pictureService.listPictureFolderBySuperId(pictureFolderParam.getSuperId(), user.getUserid());
        return Result.success(pictureFolderList);
    }

    /**
     * 创建图片库文件夹
     * @param pictureFolderParam
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "创建图片库文件夹")
    @RequestMapping("createPictureFolder")
    @PreAuthorize("hasAnyAuthority('tupian_muluguanli')")
    public Result createPictureFolder(@RequestBody PictureFolderParam pictureFolderParam, @CurrentUser Users user){
        if(pictureFolderParam.getSuperId()==null || pictureFolderParam.getFolderName()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(pictureFolderParam.getFolderName().length()>50){
            return Result.error("图片目录名称不能超过50个字", "图片目录名称不能超过50个字");
        }
        return pictureService.createPictureFolder(pictureFolderParam, user.getUserid());
    }

    /**
     * 删除图片库文件夹
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除图片文件夹")
    @RequestMapping("deletePictureFolder")
    @PreAuthorize("hasAnyAuthority('tupian_muluguanli')")
    public Result deletePictureFolder(@RequestBody PictureFolderParam pictureFolderParam, @CurrentUser Users user){
        return pictureService.deletePictureFolder(pictureFolderParam.getId());
    }

    /**
     * 批量上传图片文件
     * @param files
     * @param folderId
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.ADD_OPERATION, operDesc = "上传图片到图片库")
    @RequestMapping("uploadPicture")
    @PreAuthorize("hasAnyAuthority('shangchuantupian')")
    public Result uploadPicture(@RequestParam(value="files")MultipartFile[] files, @RequestParam(value="pictureAlts", required=false)String[] pictureAlts,
                                @RequestParam(value="pictureInfos", required=false)String[] pictureInfos, @RequestParam(value="folderId", required=false)Integer folderId,
                                @RequestParam(value="pictureSource", required=false)String pictureSource, @RequestParam(value="photographer", required=false)String photographer,
                                @CurrentUser Users user){
        for(MultipartFile multipartFile:files){
            if(multipartFile.getOriginalFilename()==null || multipartFile.getOriginalFilename().length()<=4){
                return Result.error("图片名称不能为空","图片名称不能为空");
            }
            if(multipartFile.getOriginalFilename().length()>34){
                return Result.error("图片名称超长，不能多于30个字符", "图片名称超长，不能多于30个字符");
            }
        }
        for(String alt:pictureAlts){
            if(alt!=null && alt.length()>100){
                return Result.error("图片alt超长，不能多于100个字符", "图片alt超长，不能多于100个字符");
            }
        }
        for(String info:pictureInfos){
            if(info!=null && info.length()>100){
                return Result.error("图片介绍超长，不能多于100个字符", "图片介绍超长，不能多于100个字符");
            }
        }
        return pictureService.uploadPicture(files, pictureAlts, pictureInfos, folderId, pictureSource, photographer, user.getUserid());
    }

    /**
     * 删除图片（实际是从图片库删除图片到回收箱）
     * @param pictureBatchParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "删除图片到回收箱")
    @RequestMapping("deletePicture2Recycle")
    @PreAuthorize("hasAnyAuthority('shanchutupian')")
    public Result deletePicture2Recycle(@RequestBody PictureBatchParam pictureBatchParam, @CurrentUser Users user){
        if(pictureBatchParam.getIds()==null||pictureBatchParam.getIds().length==0){
            return Result.error("缺失图片id参数");
        }

        return pictureService.deletePicture2Recycle(pictureBatchParam.getIds());
    }

//    /**
//     * 从回收箱删除图片
//     * @param pictureBatchParam
//     * @param user
//     * @return
//     */
//    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "从回收箱删除图片")
//    @RequestMapping("deletePictureFromRecycleBin")
//    public Result deletePictureFromRecycleBin(@RequestBody PictureBatchParam pictureBatchParam, @CurrentUser Users user){
//        if(user==null){
//            return Result.error("用户未认证登录");
//        }
//        if(pictureBatchParam.getIds()==null||pictureBatchParam.getIds().length==0){
//            return Result.error("缺失图片id参数");
//        }
//
//        return pictureService.deletePictureFromRecycle(pictureBatchParam.getIds());
//    }


    /**
     * 修改图片名称
     * @param pictureParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改图片名称")
    @RequestMapping("updatePictureName")
    @PreAuthorize("hasAnyAuthority('xiugaitupianxinxi')")
    public Result updatePictureName(@RequestBody PictureParam pictureParam, @CurrentUser Users user){
        if(pictureParam.getId()==null){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        if(pictureParam.getPictureName()==null || pictureParam.getPictureName().length()<=4){
            return Result.error("图片名称不能为空","图片名称不能为空");
        }
        if(pictureParam.getPictureName()!=null && pictureParam.getPictureName().length()>34){
            return Result.error("图片名称超长，不能多于30个字符", "图片名称超长，不能多于30个字符");
        }
        if(pictureParam.getPictureAlt()!=null && pictureParam.getPictureAlt().length()>100){
            return Result.error("图片alt超长，不能多于100个字符", "图片alt超长，不能多于100个字符");
        }
        if(pictureParam.getPictureInfo()!=null && pictureParam.getPictureInfo().length()>100){
            return Result.error("图片介绍超长，不能多于100个字符", "图片介绍超长，不能多于100个字符");
        }
        return pictureService.updatePictureName(pictureParam.getId(), pictureParam.getPictureName());
    }

    /**
     * 修改图片标签
     * @param pictureParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改图片标签")
    @RequestMapping("updatePictureAlt")
    @PreAuthorize("hasAnyAuthority('xiugaitupianxinxi')")
    public Result updatePictureAlt(@RequestBody PictureParam pictureParam, @CurrentUser Users user){
        if(pictureParam.getId()==null){
            return Result.error("缺失图片id参数");
        }
        if(pictureParam.getPictureAlt().length()>100){
            return Result.error("图片alt超长，不能多于100个字符", "图片alt超长，不能多于100个字符");
        }
        return pictureService.updatePictureAlt(pictureParam.getId(), pictureParam.getPictureAlt());
    }

    /**
     * 修改图片信息
     * @param pictureParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改图片摄影记者")
    @RequestMapping("updatePictureInfo")
    @PreAuthorize("hasAnyAuthority('xiugaitupianxinxi')")
    public Result updatePictureInfo(@RequestBody PictureParam pictureParam, @CurrentUser Users user){
        if(pictureParam.getId()==null){
            return Result.error("缺失图片id参数");
        }
        if(pictureParam.getPictureInfo().length()>100){
            return Result.error("图片介绍超长，不能多于100个字符", "图片介绍超长，不能多于100个字符");
        }
        return pictureService.updatePictureInfo(pictureParam.getId(), pictureParam.getPictureInfo());
    }

    /**
     * 修改图片摄影作者
     * @param pictureParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改图片信息")
    @RequestMapping("updatePhotographer")
    @PreAuthorize("hasAnyAuthority('xiugaitupianxinxi')")
    public Result updatePhotographer(@RequestBody PictureParam pictureParam, @CurrentUser Users user){
        if(pictureParam.getId()==null){
            return Result.error("缺失图片id参数");
        }
        if(pictureParam.getPhotographer().length()>16){
            return Result.error("摄影记者超长，不能多于16个字符", "摄影记者超长，不能多于16个字符");
        }
        return pictureService.updatePhotographer(pictureParam.getId(), pictureParam.getPhotographer());
    }

    /**
     * 更新图片使用状态
     * @param pictureParam
     * @param user
     * @return
     */
//    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "修改图片使用状态")
//    @RequestMapping("updatePictureIsUse")
//    public Result updatePictureIsUse(@RequestBody PictureParam pictureParam, @CurrentUser Users user){
//        if(user==null){
//            log.warn("用户未认证登录请求/picture/updatePictureIsUse接口");
//            return Result.error("用户未认证登录");
//        }
//        if(pictureParam.getId()==null){
//            return Result.error("缺失图片id参数");
//        }
//        return pictureService.updatePictureIsUse(pictureParam.getId(), pictureParam.getPictureIsUse());
//    }

    /**
     * 移动图片
     * @param pictureBatchParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "移动图片")
    @RequestMapping("movePicture")
    @PreAuthorize("hasAnyAuthority('yidongtupian')")
    public Result movePicture(@RequestBody PictureBatchParam pictureBatchParam, @CurrentUser Users user){
        if(pictureBatchParam.getIds().length==0){
            return Result.error("缺失图片id参数");
        }
        return pictureService.movePicture(pictureBatchParam.getIds(), pictureBatchParam.getTargetFolderId());
    }

    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.UPDATE_OPERATION, operDesc = "从回收箱恢复图片")
    @RequestMapping("restorePicture")
    @PreAuthorize("hasAnyAuthority('tupianhuishouzhanguanli')")
    public Result restorePicture(@RequestBody PictureBatchParam pictureBatchParam, @CurrentUser Users user){
        if(pictureBatchParam.getIds().length==0){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return pictureService.restorePicture(pictureBatchParam.getIds());
    }


    /**
     * 从回收箱删除图片
     * @param pictureBatchParam
     * @param user
     * @return
     */
    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "从回收箱彻底删除图片")
    @RequestMapping("deletePictureFromRecycle")
    @PreAuthorize("hasAnyAuthority('tupianhuishouzhanguanli')")
    public Result deletePictureFromRecycle(@RequestBody PictureBatchParam pictureBatchParam, @CurrentUser Users user){
        if(pictureBatchParam.getIds().length==0){
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        return pictureService.deletePictureFromRecycle(pictureBatchParam.getIds());
    }

    @RequestMapping("getPictureTotalSize")
    @PreAuthorize("hasAnyAuthority('tupian_quankuchaxun','xiugaitupianxinxi')")
    public Result getPictureTotalSize(@CurrentUser Users user){
        return pictureService.getPictureTotalSize();
    }

    @RequestMapping("usePicture")
    @PreAuthorize("hasAnyAuthority('tupian_quankuchaxun','xiugaitupianxinxi')")
    public Result usePicture(@RequestBody PictureUseParam pictureUseParam, @CurrentUser Users user){
        return pictureService.usePicture(pictureUseParam);
    }

    @RequestMapping("queryPictureInRecycle")
    @PreAuthorize("hasAnyAuthority('tupian_quankuchaxun')")
    public Result queryPictureInRecycle(@RequestBody PictureRecycleQueryParam pictureRecycleQueryParam){
        PageInfo<Picture> pageInfoPicture = pictureService.queryPictureInRecycle(pictureRecycleQueryParam);
        return Result.success(pageInfoPicture);
    }

    @OperLog(operModule = NewsConstant.PICTURE_MODULE, operType = NewsConstant.DELETE_OPERATION, operDesc = "清空图片回收站")
    @RequestMapping("clearRecycle")
    @PreAuthorize("hasAnyAuthority('tupianhuishouzhanguanli')")
    public Result clearRecycle(@CurrentUser Users user){
        return pictureService.clearRecycle();
    }
}
